﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QFlow.Data;
using Microsoft.Reporting.WinForms;

namespace QFlow.Reports
{
    public partial class ProfitReportViewer : Utils.BaseForm
    {
        public ProfitReportViewer(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
        }

        private void ProfitReportViewer_Load(object sender, EventArgs e)
        {
            cmbProducts.DataSource = this.Context.Products.Where(product => !product.IsNonStockable);
            cmbProducts.SelectedIndex = -1;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int receivedItemTypeId = this.Context.ItemTypes.GetReceivedType().ItemTypeID;
            int closedStatusId = this.Context.OrderStatus.GetClosedStatus().StatusID;

            int productId = (cmbProducts.SelectedIndex == -1 ? 0 : (int)cmbProducts.SelectedValue);

            DateTime fromDate = dtFrom.Value.Date;
            DateTime toDate = dtTo.Value.Date;

            var result = from product in this.Context.StockableProducts
                         join sItem in this.Context.SaleItems
                         on product.ProductID equals sItem.ProductID
                         join sOrder in this.Context.SaleOrders
                         on sItem.SalesOrderID equals sOrder.SaleOrderID
                         where
                         (productId == 0 ? true : sItem.ProductID == productId)
                         &&
                         sOrder.Date.Date >= fromDate
                         &&
                         sOrder.Date.Date <= toDate

                         orderby product.ProductID
                         group sItem by sItem.StockableProduct into g
                         select new ProfitReportResult
                         {
                             ProductId = g.Key.ProductID,
                             ProductName = g.Key.Product.Name,
                             ItemsNumber = g.Count(),
                             Profit = g.Sum(item => item.Quantity * (item.UnitPrice - item.StockableProduct.PurchaseUnitPrice))
                         };

            ProfitReportResultBindingSource.DataSource = result.ToArray();

            List<Microsoft.Reporting.WinForms.ReportParameter> repeortParams = new List<Microsoft.Reporting.WinForms.ReportParameter>();
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("FromDate",fromDate.ToString("dd-MM-yyyy")));
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("ToDate", toDate.ToString("dd-MM-yyyy")));
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("Product", (productId != 0 ? cmbProducts.Text : "All")));

            reportViewer1.LocalReport.SetParameters(repeortParams);
            reportViewer1.RefreshReport();
        }
    }

    public class ProfitReportResult
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public int ItemsNumber { get; set; }
        public double Profit { get; set; }
    }
}
